<?php

	/**
	 * @example
	 *	addOn('kat_ref', 'sql_ref', 'Kategorien Zugehoerigkeit', array(
	 *		'ref_table' => array(
	 *			'table' => 'test_cata_ref',
	 * 			'data_id' => 'cata_id',
	 * 			'ref_id' => 'fahrzeug_id'
	 *		),
	 *		'data_table' => array(
	 *			'table' => 'test_cata',
	 *			'value' => 'id',
	 *			'title' => 'name'
	 *		)
	 *	);
	 */
	class Devmod_method_addOn extends Devmod_methods_abstraction {

		protected $after_operation = array(array(
										'for_method' => 'export',
										'for_method_type' => 'edit',
										'use' => 'eval_edit'
									), array(
										'for_method' => 'export',
										'for_method_type' => 'add',
										'use' => 'eval_add'
									));
		
		public function set_config($args)
		{
			$this->update_config('addons', array($args[0] => array(
				'identifier' => $args[0],
				'addon_type' => $args[1],
				'addon_title' => $args[2],
				'args' => $args[3]
			)));
		}
		
		public function eval_add($args)
		{
			if(!isset($this->config[$this->type]['addons'])) return $args;
			
			foreach($this->config[$this->type]['addons'] as $key => $item)
			{
				$this->call_class($item['identifier'], array());
			}
			
			return $args;
		}
		
		public function eval_edit($args)
		{			
			if(!isset($this->config[$this->type]['addons'])) return $args;
			
			foreach($this->config[$this->type]['addons'] as $key => $item)
			{
				$this->call_class($item['identifier'], $args);
			}
			
			return $args;
		}
		
		private function call_class($identifier, $row_value = null)
		{
			$_cfg = $this->config[$this->type]['addons'][$identifier];
			
			$class = "Devmod_method_addOn_" . $_cfg['addon_type'];
			
			$_tmp_obj = new $class($row_value, $_cfg, $this->config, $this->view);
			
			if(method_exists($_tmp_obj, 'type_' . $this->type))
			{
				return call_user_func_array(array($_tmp_obj, 'type_' . $this->type), array());
			} else {
				$err = new Yomm_Error("[devmod error: addOn class type '{$class}' does not have 'type_{$this->type}' method]");
				echo $err->getMessage();
			}
		}
		
	}